<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Logic\Sms;
use App\Logic\Code;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Models\Admin;


class ConfigController extends Controller
{

    /**
     * @OA\Post(
     *     path="/admin/config_Level",
     *     tags={"后台配置"},
     *     summary="等级配置修改",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"id","level","level_note","capacity","calculate"},
     *                 @OA\Property(property="id", type="number", description="id"),
     *                 @OA\Property(property="level", type="number", description="等级"),
     *                 @OA\Property(property="level_note", type="string", description="等级称号"),
     *                 @OA\Property(property="capacity", type="string", description="capacity"),
     *                 @OA\Property(property="calculate", type="string", description="calculate"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function level(Request $request)
    {

        $post_data = $request->all();

        $validateRule = [
            'id' => 'required',
            'level' => 'required',
            'level_note' => 'required',
            'capacity' => 'required|numeric',
            'calculate' => 'required|numeric',
        ];
        $errorMsg = [
            'id.required' => '请传入id',
            'level.required' => '请传入等级',
            'level_note.required' => '请传入等级说明',
            'capacity.required' => '非法capacity',
            'capacity.numeric' => '非法capacity',
            'calculate.required' => '非法calculate',
            'calculate.numeric' => '非法calculate',
        ];

        $validator = Validator::make($post_data, $validateRule, $errorMsg);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            return e_msg(300001, $errors[0]);
        }


        $update = [
            'level_note' => $post_data['level_note'],
            'capacity' => $post_data['capacity'],
            'calculate' => $post_data['calculate'],
        ];

        $rel = DB::table('config_level')
            ->where('id',$post_data['id'])
            ->update($update);
        if( $rel ){
             Cache::forget('config_level');
             return s_msg('');
        }else{
            return e_msg(300015,'您未作出改变');
        }

    }







    /**
     * @OA\Post(
     *     path="/admin/config_products",
     *     tags={"后台配置"},
     *     summary="矿机配置修改",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"id","name","content","token","capacity"},
     *                 @OA\Property(property="id", type="number", description="id"),
     *                 @OA\Property(property="name", type="string", description="名称"),
     *                 @OA\Property(property="content", type="string", description="描述"),
     *                 @OA\Property(property="token", type="string", description="token"),
     *                 @OA\Property(property="capacity", type="number", description="容量"),
     *                 @OA\Property(property="days", type="number", description="周期"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function products(Request $request)
    {

        $post_data = $request->all();

        $validateRule = [
            'id' => 'required',
            'name' => 'required',
            'content' => 'required',
            'token' => 'required',
            'capacity' => 'required|numeric',
            'days' => 'required|numeric',
        ];
        $errorMsg = [
            'id.required' => '请传入id',
            'name.required' => '请传入name',
            'content.required' => '请传入content',
            'token.required' => '请传入token',
            'capacity.required' => '请传入capacity',
            'capacity.numeric' => '非法capacity',
            'days.required' => '请传入days',
            'days.numeric' => '非法days',
        ];

        $validator = Validator::make($post_data, $validateRule, $errorMsg);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            return e_msg(300001, $errors[0]);
        }

        $update = [
            'name' => $post_data['name'],
            'content' => $post_data['content'],
            'token' => $post_data['token'],
            'capacity' => $post_data['capacity'],
            'days' => $post_data['days'],
        ];

        $rel = DB::table('config_products')
            ->where('id',$post_data['id'])
            ->update($update);
        if( $rel ){
            Cache::forget('config_products');
            return s_msg('');
        }else{
            return e_msg(300015,'您未作出改变');
        }

    }

    /**
     * @OA\Post(
     *     path="/admin/config_sys",
     *     tags={"后台配置"},
     *     summary="系统配置修改",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"id","type","key","val","note"},
     *                 @OA\Property(property="id", type="number", description="id"),
     *                 @OA\Property(property="val", type="string", description="值"),
     *                 @OA\Property(property="note", type="strign", description="描述"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function sys(Request $request)
    {

        $post_data = $request->all();

        $validateRule = [
            'id' => 'required',
            'val' => 'required',
            'note' => 'required',
        ];
        $errorMsg = [
            'id.required' => '请传入id',
            'val.required' => '请传入值',
            'note.required' => '请传入描述',
        ];

        $validator = Validator::make($post_data, $validateRule, $errorMsg);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            return e_msg(300001, $errors[0]);
        }

        $update = [
            'val' => $post_data['val'],
            'note' => $post_data['note'],
        ];

        $rel = DB::table('config_sys')
            ->where('id',$post_data['id'])
            ->update($update);
        if( $rel ){
            Cache::forget('config_sys');
            Cache::forget('configs');
            return s_msg('');
        }else{
            return e_msg(300015,'您未作出改变');
        }

    }






}
